package ch.usi.inf.pf2.cell;

import ch.usi.inf.pf2.node.*;

import junit.framework.TestCase;


public class ParserTest extends TestCase {

	public void testIntNumber() {
		Parser p = new Parser(null);
		Node n = p.parseFormula("223");
		// assert that we have no syntax errors
		assertNotNull(n); 
		Value v = n.evaluate();
		// assert that we have no runtime errors
		assertNotNull(v);
		// (implicitly) assert that we get a value of the correct type (cast would throw a ClassCastException otherwise)
		NumericValue nv = (NumericValue)v;
		// assert that we get the correct value
		assertEquals(223.0, nv.get());
		
	}

	public void testFloatNumber() {
		Parser p = new Parser(null);
		Node n = p.parseFormula("0.9");
		assertNotNull(n);
		Value v = n.evaluate();
		assertNotNull(v);
		NumericValue nv = (NumericValue)v;
		assertEquals(0.9, nv.get());
	}

	public void testAddition() {
		Parser p = new Parser(null);
		Node n = p.parseFormula("12+3.1");
		assertNotNull(n);
		Value v = n.evaluate();
		assertNotNull(v);
		NumericValue nv = (NumericValue)v;
		assertEquals(15.1, nv.get());
	}

	public void testSubtraction() {
		Parser p = new Parser(null);
		Node n = p.parseFormula("12-3");
		assertNotNull(n);
		Value v = n.evaluate();
		assertNotNull(v);
		NumericValue nv = (NumericValue)v;
		assertEquals(9.0, nv.get());
	}

	public void testAdditionAndSubtraction() {
		Parser p = new Parser(null);
		Node n = p.parseFormula("1-2+3-4");
		assertNotNull(n);
		Value v = n.evaluate();
		assertNotNull(v);
		NumericValue nv = (NumericValue)v;
		assertEquals(-2.0, nv.get());
	}
	public void testMultiplication() {
		Parser p = new Parser(null);
		Node n = p.parseFormula("2*30");
		assertNotNull(n);
		Value v = n.evaluate();
		assertNotNull(v);
		NumericValue nv = (NumericValue)v;
		assertEquals(60.0, nv.get());
	}
	public void testDivision() {
		Parser p = new Parser(null);
		Node n = p.parseFormula("15/5");
		assertNotNull(n);
		Value v = n.evaluate();
		assertNotNull(v);
		NumericValue nv = (NumericValue)v;
		assertEquals(3.0, nv.get());
		
	}
	public void testDivMulAddSub() {
		Parser p = new Parser(null);
		Node n = p.parseFormula("2*50+2-40/2");
		assertNotNull(n);
		Value v = n.evaluate();
		assertNotNull(v);
		NumericValue nv = (NumericValue)v;
		assertEquals(82.0, nv.get());
	}
	public void testMultiplicationAndDivision() {
		Parser p = new Parser(null);
		Node n = p.parseFormula("10/2*30/5");
		assertNotNull(n);
		Value v = n.evaluate();
		assertNotNull(v);
		NumericValue nv = (NumericValue)v;
		assertEquals(30.0, nv.get());
	}
	public void testGroup() {
		Parser p = new Parser(null);
		Node n = p.parseFormula("(6+40)*4/8");
		assertNotNull(n);
		Value v = n.evaluate();
		assertNotNull(v);
		NumericValue nv = (NumericValue)v;
		assertEquals(23.0, nv.get());
	}

}
